package com.css.dzj.dzjdzqs.common.menu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjdzqs.common.util.R;
import com.css.dzj.dzjdzqs.common.util.Restful.RestfulUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/org")
public class GetOrgController {
    @Autowired
    private HttpServletRequest request;
    @Value("${BASE_URL}")
    private String BASE_URL;


    @GetMapping("/getOrg")
    public R getMenu() {
        JSONArray data = listOrgMenu();
        return R.ok().put("data", data);
    }

    /**
     * 获取组织树
     *
     * @return
     */
    public JSONArray listOrgMenu() {
        // 根据root获取第一个组织机构（或者根据组织ID获取此机构信息）
        HashMap map = (HashMap) this.getChildOrg("root").get(0);
        String deptId = map.get("deptId").toString();
        JSONArray org = getOrgMenuChildren(deptId);
        return org;
    }

    /**
     * 递归获取组织机构信息
     *
     * @param deptId 组织ID
     * @return
     */
    public JSONArray getOrgMenuChildren(String deptId) {
        JSONArray treeDatas = null;
        JSONArray sysOrgs = JSONArray.parseArray(JSON.toJSONString(getChildOrg(deptId)));
        if (sysOrgs != null && sysOrgs.size() > 0) {
            treeDatas = new JSONArray();
            for (int i = 0; i < sysOrgs.size(); i++) {
                JSONObject sysOrg = (JSONObject) sysOrgs.get(i);
                JSONObject treeData = new JSONObject();
                treeData.put("deptId", sysOrg.get("deptId"));
                treeData.put("deptName", sysOrg.get("deptName"));
                treeData.put("deptLevel", sysOrg.get("deptLevel"));
                treeData.put("deptCode", sysOrg.get("deptCode"));
                treeData.put("state", "closed");
                JSONArray treeChildren = getOrgMenuChildren(sysOrg.get("deptId").toString());
                if (null != treeChildren) {
                    treeData.put("children", treeChildren);
                } else {
                    //没有子节点  不需要设置开启或关闭的状态
                    treeData.put("state", null);
                }
                treeDatas.add(treeData);
            }
        }
        return treeDatas;
    }


    /**
     * 根据组织机构ID获取组织机构信息
     *
     * @param deptId
     * @return
     */
    public Map getOrg(String deptId) {
        String url = BASE_URL + "/platform/api/org/depts" + deptId;
        HttpMethod method = HttpMethod.GET;
        Map map = new HashMap();
        Map body = (Map) RestfulUtil.sendRestful(request, map, url, method).get("data");
        return body;
    }

    /**
     * 根据组织机构ID获取子组织机构
     *
     * @param deptId
     * @return
     */
    public List getChildOrg(String deptId) {
        String url = BASE_URL + "/platform/api/org/depts/" + deptId + "/subdepts";
        HttpMethod method = HttpMethod.GET;
        Map map = new HashMap();
        List body = (List) RestfulUtil.sendRestful(this.request, map, url, method).get("data");
        return body;
    }
}
